package com.wyk.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyk.common.result.Result;
import com.wyk.model.hosp.HospitalSet;
import com.wyk.service.HospService;
import com.wyk.utils.MD5;
import com.wyk.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * @author WangYuKai
 * @Date 2022/8/9
 * @apiNote
 */
@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospController {
    //注入service
    @Autowired
    private HospService hospService;

    //查询医院接口的全部数据
    @ApiOperation(value = "获取所有医院设置信息")
    @GetMapping("findAll")
    public Result getAll() {
        List<HospitalSet> list = hospService.list();
        System.out.println("我执行了");
        return Result.ok(list);
    }

    //根据id逻辑删除
    @DeleteMapping("{id}")
    @ApiOperation(value = "根据id逻辑删除")
    public Result removeById(@PathVariable("id") Long id) {
        boolean b = hospService.removeById(id);
        if (b) {
            return Result.ok();
        }
        return Result.fail();
    }

    //分页查询
    @PostMapping("/pageAll/{current}/{limit}")
    @ApiOperation("分页查询")
    public Result getPageAll(@PathVariable("current") Integer current,
                             @PathVariable("limit") Integer limit,
                             @RequestBody(required = false) HospitalSetQueryVo vo) {
        Page<HospitalSet> page = new Page<HospitalSet>(current, limit);
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(vo.getHosname())){
            wrapper.like("hosname", vo.getHosname());
        }
        if(!StringUtils.isEmpty(vo.getHoscode())) {
            wrapper.eq("hoscode", vo.getHoscode());
        }
        Page<HospitalSet> page1 = hospService.page(page, wrapper);
        if(page1 != null){
            return Result.ok(page1);
        }
        return Result.fail();
    }

    //添加医院设置
    @ApiOperation("添加医院设置")
    @PostMapping("addHospSet")
    public Result addHospSet(@RequestBody HospitalSet hospitalSet) {
        //设置状态：0：不可用，1；可用
        hospitalSet.setStatus(1);
        //签名秘钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        System.out.println(hospitalSet.getSignKey());
        boolean save = hospService.save(hospitalSet);
        if (save) {
            return Result.ok();
        }
        return Result.fail();
    }
    //根据id获取医院详情
    @ApiOperation("获取指定医院信息")
    @GetMapping("/getHospById/{id}")
    public Result getHospById(@PathVariable("id")Integer id){
        HospitalSet byId = hospService.getById(id);
        if(!StringUtils.isEmpty(byId)){
            return Result.ok(byId);
        }
        return Result.fail();
    }
    @ApiOperation("修改医院设置")
    @PutMapping("updateHospSet")
    //修改医院设置
    public Result updateHospSet(@RequestBody HospitalSet hospitalSet){
        boolean save = hospService.save(hospitalSet);
        if(save){
            return Result.ok();
        }
        return Result.fail();
    }

    //批量删除医院设置
    @ApiOperation("batchRemve")
    @DeleteMapping("/batchRemve")
    public Result BatchRemove(@RequestBody List<Long> ids){
        boolean b = hospService.removeByIds(ids);
        if(b){
            return Result.ok();
        }
        return Result.fail();
    }

    //医院设置的解锁和锁定
    @ApiOperation("医院设置的解锁和锁定")
    @PutMapping("/updateStatus/{id}/status")
    public Result updateStatus(@PathVariable("id")Long id,@RequestParam("status") Integer status){
        HospitalSet hospitalSet = hospService.getById(id);
        hospitalSet.setStatus(status);
        boolean update = hospService.updateById(hospitalSet);
        if(update){
            return Result.ok();
        }
        return Result.fail();
    }

    //发送签名秘钥(使用短信)
    @ApiOperation("发送秘钥")
    @PostMapping("/sendKey/{id}")
    public Result senKey(@PathVariable("id") Long id){
        HospitalSet byId = hospService.getById(id);
        String signKey = byId.getSignKey();
        return Result.ok(signKey);
    }
}
